package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.rank.UnionActivityRankListModel;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_GENERAL_ACTIVITY_UNION_RANK, desc = "商会排名")
public class GeneralActivityUnionRankCmd extends AbstractCommand {
    private int activityId;
    private int conditionType;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdActivityUnionRankListReqMsg reqMsg = StrongholdWarProto.StrongholdActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (null == activityInfo) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder respMsg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());//空列表
        if(ActivityHelper.isUnionRankType(conditionType) && !ActivityHelper.conditionTypeIsCrossRank(conditionType)){
            UnionActivityConditionData conditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, conditionType, player.getUserId());
            String unionId = "";
            if (conditionData != null) {
                unionId = conditionData.getUnionUid();
            }
            UnionActivityRankListModel unionActivityRankListModel = GeneralActivityRankMgr.getUnionRankListModel(activityId, conditionType);
            if(unionActivityRankListModel != null){
                respMsg.setMyRank(unionActivityRankListModel.getMyRank(unionId));
                //respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());//空列表
                respMsg.setValue(unionActivityRankListModel.getMyUnionValue(unionId).toString());
            }
        }
        player.sendPacket(Protocol.U_GENERAL_ACTIVITY_UNION_RANK, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionActivityRankProto.UnionActivityRankListRespMsg.Builder respMsg = UnionActivityRankProto.UnionActivityRankListRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setMyRank(-1);
            respMsg.setValue("0");
            respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());
            player.sendPacket(Protocol.U_GENERAL_ACTIVITY_UNION_RANK_LIST, respMsg);
        }
    }
}
